package server.mapper;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.List;

/**
 * @ClassName: Server
 * @Description: 解析server.xml 信息
 * @Author: wuguizhen
 * @Date: 2020/3/31 13:23
 * @Version: v1.0 文件初始创建
 */
public class Server {

    //端口号
    private int port;

    private Mapper mapper;

    public Server() throws Exception{
        InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("server.xml");
        SAXReader saxReader = new SAXReader();
        try {
            Document document = saxReader.read(inputStream);
            Element rootElement = document.getRootElement();
            List<Element> serviceNodes = rootElement.selectNodes("//Service");
            /** 简易版只取 第一个配置 */
            Element element = serviceNodes.get(0);
            //<Connector port="8080"/>
            Element connector = (Element)element.selectSingleNode("Connector");
            String port = connector.attributeValue("port");
            //<Engine/>
            Element Engine = (Element)element.selectSingleNode("Engine");
            //<Host/>
            Element host = (Element)Engine.selectSingleNode("Host");
            String name = host.attributeValue("name");
            String appBase = host.attributeValue("appBase");
            /** 设置端口 */
            setPort(Integer.valueOf(port));
            /** 初始化映射 */
            setMapper(new Mapper(name,appBase));
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    public Mapper getMapper() {
        return mapper;
    }

    public void setMapper(Mapper mapper) {
        this.mapper = mapper;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }


}
